<template>
  <section class="login">
    <mt-header title="登录">
      <mt-button slot="left" icon="back" @click="$router.go(-1)"></mt-button>
      <mt-button slot="right" @click="submit">确定</mt-button>
    </mt-header>
    <mt-field label="用户名" placeholder="请输入用户名" v-model.trim="account"></mt-field>
    <mt-field label="密码" placeholder="请输入密码" type="password" v-model.trim="password"></mt-field>
    <div class="register">
      没有账号？
      <router-link to='/register'>去注册</router-link>
    </div>
  </section>
</template>

<script>
import { Toast } from 'mint-ui';

export default {
  name: '',
  data() {
    return {
      password: '',
      account: ''
    }
  },
  methods: {
    submit() {
      if (!this.account) {
        Toast('请输入用户名')
        return false
      } else if (!this.password) {
        Toast('请输入密码')
        return false
      }

      this.$http.postParams('/user/login', {
        username: this.account,
        pwd: this.password
      }).then((data) => {
        if (data.code === 0) {
          UT.setSessionJson('CUR_USER', data.data)
          this.$router.push({ name: 'home' })
        } else {
          Toast('密码错误')
        }
      })
    }
  },
  watch: {},
  mounted() { }
}
</script>

<style lang="less">
@import "../assets/less/app.variable.less";

.login {
  .register {
    padding: 5px 10px;
    a {
      color: @primary;
    }
  }
}
</style>
